package com.xhlj.controller;

import com.xhlj.annotation.SystemLog;
import com.xhlj.controller.request_body.LoginBody;
import com.xhlj.service.impl.LoginService;
import com.xhlj.utils.RegexUtils;
import com.xhlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("login")
    /*用户名密码登录*/
    public Result login(@RequestBody LoginBody loginBody) {
        /*
         * 因为security自带的验证只针对form-data,我们要自己实现一个requestBody的
         * */
        Result res = loginService.login(loginBody.getPhoneNumber(), loginBody.getPassword());
        System.out.println("res = " + res);
        return res;
    }

    /*username +captcha
    手机号验证码登录*/
    @SystemLog(title = "登录模块", content = "验证码登录")
    @PostMapping("login/captcha")
    public Result loginBySms(@RequestBody LoginBody loginBody) {
//       TODO: 1. 校验当前表单内的的手机号格式
        if (!RegexUtils.isPhoneInvalid(loginBody.getPhoneNumber())) {
            Result res = loginService.loginBySms(loginBody.getPhoneNumber(), loginBody.getCaptcha());
            return res;
        }
        return Result.build(null, 400, "手机号格式错误");
    }

    @SystemLog(title = "登录模块", content = "请求验证码")
    @GetMapping("login/getcaptcha")
    public Result getCaptcha(String phoneNumber) {
//      1. 校验当前的手机号
        Result res = loginService.sendCaptcha(phoneNumber);
        return res;
    }

    @SystemLog(title = "登录模块", content = "用户注册")
    @PostMapping("/register")
    /*使用手机号注册账号*/
    public Result register(@RequestBody LoginBody loginBody) {
        Result res = loginService.register(loginBody);
        return res;
    }


}
